﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Secams.utils;
using System.Data.OleDb;
using ADODB;
using System.Data;
using Secams.model;

namespace Secams.controller
{
    class ActionTraceDao
    {
        public const string TABLENAME = "datActionTrace";
        public string _dbFilepath;
        
        public ActionTraceDao(string dbFilepath)
        {
            if (dbFilepath != null)
                _dbFilepath = dbFilepath;
            else
                _dbFilepath = "../../data/Secams.mdb";
        }

        public bool initialize() {
            bool retval = false;
            using (OleDbConnection dbConn = MdbUtil.getConnection(_dbFilepath))
            {

                if (!MdbUtil.existsTable(dbConn, TABLENAME))
                {
                    //http://www.sulc.edu/sulcalumni/app/lib/pear/docs/MDB/doc/datatypes.html#5.1.3
                    OleDbCommand cmd = dbConn.CreateCommand();
                    cmd.CommandText = "CREATE TABLE "+TABLENAME+" (participantId TEXT(32)"
                        + ", scenarioOrder TEXT(64)"
                        + ", scenarioVersion TEXT(64)"
                        + ", trialNum INT" // trial number
                        + ", actionNumber INT" // increment per trial
                        + ", actionElement TEXT(127)" // Ex: Left Button
                        + ", actionPerformed TEXT(255)" // Ex: Pressed
                        + ", actionDescription TEXT(255) " // Left Button Pressed
                        + ", remark MEMO" // 
                        + ", registDt DATETIME null);";
                    cmd.ExecuteNonQuery();
                    retval = true;
                }
                dbConn.Close();
            }

            return retval;
        }

        public void insert(TraceAction action)
        {
            using (OleDbConnection dbConn = MdbUtil.getConnection(_dbFilepath))
            {
                OleDbCommand insertCommand = new OleDbCommand(
                    "INSERT INTO " + TABLENAME + " (participantId, scenarioOrder, scenarioVersion, trialNum, actionNumber, actionElement, actionPerformed, actionDescription, remark, registDt )"
                    + " values (@participantId, @scenarioOrder, @scenarioVersion, @trialNum, @actionNumber, @actionElement, @actionPerformed, @actionDescription, @remark, @registDt)", dbConn);

                insertCommand.Parameters.Add("@participantId", action.participantId);
                insertCommand.Parameters.Add("@scenarioOrder", action.scenarioOrder);
                insertCommand.Parameters.Add("@scenarioVersion", action.scenarioVersion);
                insertCommand.Parameters.Add("@trialNum", action.trialNum);
                insertCommand.Parameters.Add("@actionNumber", action.actionNumber);
                insertCommand.Parameters.Add("@actionElement", action.actionElement);
                insertCommand.Parameters.Add("@actionPerformed", action.actionPerformed);
                insertCommand.Parameters.Add("@actionDescription", action.actionDescription);
                insertCommand.Parameters.Add("@remark", action.remark);
                // Check following line
                insertCommand.Parameters.Add("@registDt", action.registDt.ToOADate());

                insertCommand.ExecuteNonQuery();
            }
        }
    }
}
